import  data  from "./data.js";
import customer from "./customer.js"

function UnfinishedFood(){
    this.person = {};
    this.food = {};
    this.done = "false";
}

var cook = {
    foods:[],
    person:[],
    employCook(){
        if(confirm("真的要雇佣一位额外的厨师吗？你将花费400元雇佣费，并且每周付给他150元的薪水。") === true){
            if(data.money < 400 ){
                alert("你没有足够的钱！")
                return 0;
            }
            data.renderMoney(-400);
            let cooks = document.querySelector(".cook");
            let nullCooker = document.getElementById("nullCooker");
            let newCook = document.createElement("div");
            newCook.dataset.state = "ready";
            newCook.className = "cooker";
            newCook.innerHTML = `<img src="./restaurant/cook.png" class="cook-img" alt=""><div class="cooker-food"></div><img src="./restaurant/iconfinder_Food-Dome_379366.png" class="cooker-finish">`;
            if(cooks.children.length === 6){
                cooks.replaceChild(newCook,nullCooker)
            }else{
                cooks.insertBefore(newCook,nullCooker)
            }
        }else{
            return 0;  
        }
    },
    manageCook(person){
        let cookers = document.getElementsByClassName("cooker");
        let personData = {}
        if(person){
            personData = JSON.parse(JSON.stringify(person));
        }
        if(JSON.stringify(personData) !== "{}"){
            for(let i = 0;i<personData.food.length;i++){
                let unfinished = new UnfinishedFood();
                unfinished.person = personData;
                unfinished.food = personData.food[i];
                cook.foods.push(unfinished);
                data.foodNumer++;
            }
        }
        //如果有新点的菜单，将信息和菜一起进队
        if(cook.foods.length === 0){
            console.log("没有菜可以做");
            return 0;
        }
        //  判断队列里还有没有没做的菜
        for(let i = 0;i<cook.foods.length;i++){
            if(cook.foods[i].done === "false"){
                for(let j = 0 ;j<cookers.length;j++){
                    if(cookers[j].dataset.state === "ready"){
                        cookers[j].dataset.state = "cooking";
                        cook.makeFoods(cookers[j],i)
                        continue;
                    }
                }
            }

        }
        //按照食物-厨师的顺序分配做菜
    },
    makeFoods(cookers,number){
        //arg1是厨师 arg2是食物信息
        cookers.childNodes[1].innerHTML = cook.foods[number].food.name;
        cookers.childNodes[1].style.visibility = "visible"
        cook.foods[number].done = "true";
        //渲染界面
        setTimeout(() => {
            cookers.childNodes[2].style.visibility = "visible"
            cookers.onclick = () => {
                cookers.dataset.state = "ready";
                cookers.childNodes[1].innerHTML = "";
                cookers.childNodes[1].style.visibility = "hidden"
                cookers.childNodes[2].style.visibility = "hidden"
                //渲染界面
                customer.checkFoods(number)
                //上菜
                cook.manageCook();
            }
        }, cook.foods[number].person.time * 1000);
    },
    //makefoods是单个厨师的做菜和点击函数，并且做完后会继续做新菜
    complete(){

    }
}
export default cook;